package com.microsoft.vision;

import java.net.URI;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

public class VisionInit {
	  private static final String subscriptionKey = "889ed626642b40dfb69efaabddf80d0a";
	  private static final String uriBase =
	            "https://eastasia.api.cognitive.microsoft.com/vision/v1.0/ocr";
      
	  public static Map<String,Integer> getVisionToFormula(String imageToAnalyze) {
	        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

	        try {
	            URIBuilder uriBuilder = new URIBuilder(uriBase);

	            uriBuilder.setParameter("language", "unk");
	            uriBuilder.setParameter("detectOrientation", "true");

	            // Request parameters.
	            URI uri = uriBuilder.build();
	            HttpPost request = new HttpPost(uri);

	            // Request headers.
	            request.setHeader("Content-Type", "application/json");
	            request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);

	            // Request body.
	            StringEntity requestEntity =
	                    new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
	            request.setEntity(requestEntity);

	            // Call the REST API method and get the response entity.
	            HttpResponse response = httpClient.execute(request);
	            HttpEntity entity = response.getEntity();

	            if (entity != null) {
	                // Format and display the JSON response.
	                String jsonString = EntityUtils.toString(entity);
	                JSONObject json = new JSONObject(jsonString);
	                JSONArray jsonarray = json.getJSONArray("regions");
	                System.out.println("REST Response:\n");
	                int length = json.getJSONArray("regions").length();
	                for (int i =0;i<length;i++)
	                {
	                	 JSONArray inn_jsonarray = jsonarray
	                     		.getJSONObject(i).getJSONArray("lines")
	                     		.getJSONObject(0).getJSONArray("words");
	                	 int inn_length = inn_jsonarray.length();
	             
	                	 for( int j=0;j<inn_length;j++)
	                	System.out.print(
	                			inn_jsonarray.getJSONObject(j).getString("text")+" ");
	                	 System.out.println();
	                }
	                
	                
	            }
	        } catch (Exception e) {
	            // Display error message.
	            System.out.println(e.getMessage());
	        }
			return null;
	    }  

}
